﻿using System;
using System.Diagnostics;
using System.Security.Principal;

namespace CommandSet.utils
{
    internal class CommandLineExecutor
    {

        public static bool IsUserAdmin()
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        public static void ExecuteCommand(string command, bool runAsAdmin)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/c " + command;
            startInfo.UseShellExecute = true;

            if (runAsAdmin)
            {
                startInfo.Verb = "runas";
            }

            Process process = new Process
            {
                StartInfo = startInfo
            };
            process.Start();
        }


        public static void ExecuteCmdFile(string filePath)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = filePath;
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = true;

            Process process = new Process();
            process.StartInfo = startInfo;
            process.Start();
        }

        public static void ExecuteCmdFileWithAdminPrivileges(string filePath)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = filePath,
                Verb = "runas", // 指定以管理员身份运行命令
                CreateNoWindow = true,
                UseShellExecute = true
            };

            Process process = new Process
            {
                StartInfo = startInfo
            };
            process.Start();
        }


        public static void ExecuteRegFile(string path)
        {
            Process regeditProcess = Process.Start("regedit.exe", "/s " + path);
            regeditProcess.WaitForExit();
        }
    }
}
